package ifpe.datasource;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;

/**
 *
 * @author Sostenes
 */
public class ThreadLocalDataSource implements DataSource {

    private Properties properties = new Properties();
    private ThreadLocal<Connection> connections = new ConnectionThreadLocal();
    private static ThreadLocalDataSource instance;

    private class ConnectionThreadLocal extends ThreadLocal<Connection> {

        @Override
        protected Connection initialValue() {
            Connection conn;
            String url = properties.getProperty("jdbc.url");
            String user = properties.getProperty("jdbc.user");
            String password = properties.getProperty("jdbc.password");
            try {
                conn = DriverManager.getConnection(url, user, password);
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            Connection connection = ConnectionProxy.newInstance(conn, instance);
            return connection;
        }
    }

    public static synchronized ThreadLocalDataSource getInstance() {
        if (instance == null) {
            instance = new ThreadLocalDataSource();
        }
        return instance;
    }

    private ThreadLocalDataSource() {
        try {
            properties.load(ThreadLocalDataSource.class.getClassLoader().getResourceAsStream("ifpe/config/jdbc.properties"));
            Class.forName(properties.getProperty("jdbc.driver"));
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex);
        }
    }

    public void removeConnection() {
        connections.remove();
    }

    @Override
    public Connection getConnection() throws SQLException {
        return connections.get();
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int getLoginTimeout() throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
